﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using InfraWEB.BLL;
using InfraWEB.Comum.Enum;

namespace InfraWEB.UI
{
	public partial class PredioLista : Base
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			CheckIfUsuarioIsLogged();
			CheckIfUsuarioHasAccessPermission(int.Parse(Enumeration.TipoUsuarioName.AdministradorIndex));
			if (!Page.IsPostBack)
			{
				showPredios();
				if ((string)Context.Items["BuildingRegisterStatus"] == "ok")
				{
					lblMensagem.Visible = true;
					lblMensagem.Text = "Prédio cadastrado com sucesso.";
				}
				else if ((string)Context.Items["BuildingEditStatus"] == "ok")
				{
					lblMensagem.Visible = true;
					lblMensagem.Text = "Prédio modificado com sucesso.";
				}
				else
				{
					lblMensagem.Visible = false;
					lblMensagem.Text = "";
				}
			}
		}

		protected void btnNovoPredio_Click(object sender, EventArgs e)
		{
			lblMensagem.Visible = false;
			lblMensagem.Text = "";
			Response.Redirect("~/PredioNovo.aspx");
		}

		protected void predioGridView_RowEditing(object sender, GridViewEditEventArgs e)
		{
			Context.Session["EditId"] = int.Parse(predioGridView.DataKeys[e.NewEditIndex].Value.ToString());
			Server.Transfer("~/PredioEdita.aspx");
		}

		protected void predioGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			int userId = int.Parse(predioGridView.DataKeys[e.RowIndex].Value.ToString());
			Predio building = new Predio();
			building.RemovePredio(userId);
			showPredios();
			lblMensagem.Visible = true;
			lblMensagem.Text = "Prédio removido com sucesso.";
		}

		protected void predioGridView_Sorting(object sender, GridViewSortEventArgs e)
		{

		}

		protected void showPredios()
		{
			Predio building = new Predio();
			predioGridView.DataSource = building.GetAllPredios();
			predioGridView.DataBind();
		}
	}
}
